package ar.com.ford.it.fullEconomics.persistence.dao.query;

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese02Econsource;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class FindEconomicSourcesQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teese02Econsource economicSource = (Teese02Econsource) parameters[0];
        Integer code = null;
        String name = null;
        Teesg01Plant plant = null;

        code = economicSource.getEese02EconsourceNumberK();
        name = economicSource.getEese02EconsourceNameN();
        plant = economicSource.getEese02PlantCodeC();

        if (name == null) {
            name = "%";
        } else {
            name += "%";
        }
        
        exp = builder.get("eese02PlantCodeC").equal(plant);
        if (code != null) {
            exp = exp.and(builder.get("eese02EconsourceNumberK").equal(code));
        } else {
            exp = exp.and(builder.get("eese02EconsourceNameN").toLowerCase().like(
                    name.toLowerCase()));
        }

        return exp;
    }

}
